/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.people.presenter;

import android.text.TextUtils;

import com.suozhang.framework.utils.T;
import com.yiqi.liebang.entity.bo.FriendBo;
import com.yiqi.liebang.entity.bo.PeopleBo;
import com.yiqi.liebang.entity.bo.SearchBo;
import com.yiqi.liebang.feature.people.contract.PeopleContract;
import com.yiqi.liebang.feature.people.dependencies.people.DaggerPeoplePresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class PeoplePresenter implements PeopleContract.Presenter {

    private PeopleContract.View mView;

    @Inject

    PeopleContract.Model mModel;


    public PeoplePresenter(PeopleContract.View mView) {
        this.mView = mView;
        DaggerPeoplePresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void searchPeople(String info) {
        if (TextUtils.isEmpty(info)){
//            T.showShort("请输入搜索关键字！");
            return;
        }
        SearchBo peopleBo = new SearchBo(info,1,10);

        mModel.searchPeople(peopleBo).compose(mView.bindToLife()).subscribe(new Observer<FriendBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(FriendBo friendBo) {
                if (friendBo!=null&&friendBo.getFriend().size()>0){
                    mView.showResetSuccess(friendBo);
                }else {
                    mView.showFriendsEmpty();
                }

                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void commitAddFriend(String friendId) {
        if (TextUtils.isEmpty(friendId)){
            T.showShort("对方ID有误！");
            return;
        }
        PeopleBo peopleBo=new PeopleBo();
        peopleBo.setApplicationUserUid(friendId);
        mModel.commitAddFriend(peopleBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showApplySuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
